package xyz.lwm.lazycat.web.resolve;

import xyz.lwm.lazycat.beans.BeanDesign;
import xyz.lwm.lazycat.beans.BeanDesignRegistry;
import xyz.lwm.lazycat.beans.BeanDesignRegistryCustomizer;
import xyz.lwm.lazycat.beans.BeanPostProcessor;
import xyz.lwm.lazycat.beans.BeansException;
import xyz.lwm.lazycat.web.annocation.Controller;

/**
 * 控制器收集
 *
 * @author lwm
 */
public class ControllerCollector implements BeanPostProcessor, BeanDesignRegistryCustomizer {

    private final ControllerResolver controllerResolver;

    private BeanDesignRegistry registry;

    public ControllerCollector(ControllerResolver controllerResolver) {
        this.controllerResolver = controllerResolver;
    }

    @Override
    public Object processAfterInitialization(Object bean, String beanName) throws BeansException {
        BeanDesign<?> design = registry.getDesign(beanName);
        // 判断并收集 @Controller 注解的类
        if (design.getAnnotations().hasAnnotation(Controller.class)) {
            controllerResolver.add(design, bean);
        }
        return bean;
    }

    @Override
    public void customize(BeanDesignRegistry beanRegistry) {
        this.registry = beanRegistry;
    }

}
